package db;

import java.sql.*;
/**
 * This class is responsible for managing the database connections from the application
 * server connection pool.
 * Usage of Connections should look like the following example:
 * <p><pre>
 *		Connection conn = null;
 *		try {
 *			conn = ConnectionManager.getConnection();
 *			// Do work .....
 *		}
 *		finally {
 *			ConnectionManager.releaseConnection(conn);
 *		}
 *	</pre></p>
 * 
 * @modelguid {BF1475E8-FFC6-4419-A963-782CEFD22F1D}
 */
public abstract class ConnectionManagerLocal {
	
	
	
	//public static int DUPLICATE_RECORD_CODE=1;
	//private static String DB = "jdbc:odbc:myschool2";	
	//private 
	private static String DB ;
    //private static String driver ="sun.jdbc.odbc.JdbcOdbcDriver";
    private static String driver ="com.mysql.jdbc.Driver";
    
	/**
	 * Name of the authorized user to connect to the database.
	 * @modelguid {8A529322-0AC7-46EC-B852-25CF0919CC67}
	 */
	//private static String user;
	/**
	 * user password.
	 * @modelguid {CA0080FC-FFD4-4B68-87C6-CE032C553081}
	 */
	//private static String password;
	
	
	
/**
 * This method returns a database connection, from the connection pool.
 * @return the connection established by the data source
 * @throws if the connection could not be returned
 * @modelguid {A6F7857C-583A-4057-98A0-226C55252B06}
 */
public static final Connection getConnection() throws SQLException,ClassNotFoundException {
	
	//return pool.getConnection();
	
	//java.sql.Connection conn = DriverManager.getConnection(url2,"root","bey2ollak");
	java.sql.Connection conn = DriverManager.getConnection(url2,"auth_user","aykalam");
	conn.setAutoCommit(true);
	return conn;
		
	/*
	Connection conn = ds.getConnection();
	// This to insure that the connection comming from the pool is always
	// having autoCommit = true.
	conn.setAutoCommit(true);
	return conn;		
	*/
}
static String url2 = null;
/**
 * Initializes the connection manager.  It uses the parameters used in the properties file
 * to lookup for a data source in the naming server.
 * @modelguid {29F50F69-A49E-4460-92F9-2C3AE51933D1}
 */
public final static void init(String url) throws Exception {	
	DB = url;
	Class.forName(driver);
	url2 = "jdbc:mysql://localhost:3306/"+DB+"?autoReconnect=true";		
}
/**
 * This method releases an allocated database connection.
 * This will be done by calling <code>Connection.close()</code>
 * which is in fact only returning the connection to the WAS connection pool.
 *
 * @param conn the connection to be released
 * @modelguid {CBAA40B1-D9C3-461A-A710-B0F6DC6E8103}
 */
final public static void releaseConnection(Connection conn) throws SQLException {
	
	if (conn != null) {
		try {
			// This is to insure that the connection is clean before returning to the pool.
			if (conn.getAutoCommit()==false)
			{
				conn.rollback();
				conn.setAutoCommit(true);
			}			
			conn.close();
			conn = null;
		}
		catch (SQLException e) {
			System.err.println("ConnectionManager.releaseConnection(Connection) Exception while releasing connection");
			throw e;
		}
	}	
}
}
